package com.feeling.oafeeling.controller;
import com.feeling.oafeeling.common.pojo.AjaxResult;
import com.feeling.oafeeling.pojo.Role;
import com.feeling.oafeeling.pojo.RoleParam;
import com.feeling.oafeeling.service.RoleService;
import com.feeling.oafeeling.utils.util.anno.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @PostMapping("")
    public AjaxResult getAllRole(@RequestBody RoleParam roleParam ){
        System.out.println("roleParam"+ roleParam);
        roleParam.setCurrentPage((roleParam.getCurrentPage() - 1) * roleParam.getPageSize());
        List<RoleParam> allRole = roleService.getAllRole(roleParam);
        List<Role> roleList = roleService.selectListAll();

        for (RoleParam role:allRole) {
            List<String> strings = roleService.queryRoleMenu(role.getRoleId());
            role.setCurrentPage(roleParam.getCurrentPage());
            role.setPageSize(roleParam.getPageSize());
            role.setTotal(allRole.size());
            role.setName(strings);
        }
        System.out.println(allRole.size() + "----size");
        return AjaxResult.success("查询成功",allRole);
    }
    @PassToken
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @PostMapping("/tree")
    public AjaxResult getRoleName(){
        List<RoleParam> roleName = roleService.getRoleName();
        System.out.println(roleName);
        return roleName.isEmpty() ? AjaxResult.error("查询成功",roleName) : AjaxResult.success(roleName);
    }

    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @PostMapping("/update")
    public AjaxResult updateRole(@RequestBody RoleParam roleParam){
        System.out.println("=================================");
        System.out.println(roleParam);
        System.out.println("=================================");
        roleService.updateRoleById(roleParam);
        return AjaxResult.success("更新成功");
    }
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @PostMapping("/insertRole")
    public AjaxResult insertRole(@RequestBody RoleParam roleParam){

        System.out.println("insertRole"+roleParam);

        roleService.insertRole(roleParam);

        return AjaxResult.success("新增成功");
    }

    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @PostMapping("/queryRoleMenu")
    public AjaxResult queryRoleMenu(@RequestBody RoleParam roleParam){
        System.out.println("roleParam.getRoleId()" + roleParam.getRoleId());
        List<String> strings = roleService.queryRoleMenu(roleParam.getRoleId());
        System.out.println("ssssssss"+strings);
        return AjaxResult.success("查询成功",strings);

    }

    @PostMapping("/delRole")
    public AjaxResult delRole(@RequestBody List<RoleParam> roleParam){

        System.out.println("roleParam" + roleParam);

        for (RoleParam role: roleParam) {
            roleService.delRole(role.getRoleId());
        }

        return AjaxResult.success("删除成功");
    }

    @PostMapping("/locksRole")
    public AjaxResult locksRole(@RequestBody List<RoleParam> roleParam){

        System.out.println("roleParam" + roleParam);

        for (RoleParam role: roleParam) {
            roleService.locksRole(role.getRoleId());
        }

        return AjaxResult.success("删除成功");
    }



}
